package com.zeng.generate.code.other;

import cn.hutool.log.StaticLog;
import com.zeng.generate.code.other.DialogueInfo;

import java.util.Arrays;

public class TestMain {

    public static void main(String[] args) {

    }

    public void test() {
        byte[] bytes = {2, 69, 48, 49, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
                42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
                42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
                42, 42, 42, 50, 48, 50, 48, 48, 53, 49, 57, 49, 50, 52, 53, 52, 57, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
                42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 49, 48, 56, 10};

//        bytes = new byte[]{2, 88, 48, 49, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
//                42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
//                42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
//                42, 42, 42, 42, 42, 42, 50, 48, 50, 48, 48, 53, 49, 57, 49, 51, 49, 54, 48, 50, 42, 42, 42, 42, 42, 42,
//                42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 49,
//                50, 49, 10};

        DialogueInfo dialogueInfo = new DialogueInfo(bytes);
        System.out.println(dialogueInfo.toString());
        System.out.println(Arrays.toString(dialogueInfo.toByteArray()));
        blockCheck(bytes, dialogueInfo.getBlockCheck());
        printlnLog(dialogueInfo);
    }

    public static boolean blockCheck(byte[] bytes, String code) {
        byte[] destBytes = new byte[126];
        System.arraycopy(bytes, 1, destBytes, 0, 126);
        String xorVal = String.valueOf(getXorVal(destBytes));
        StaticLog.info("original {}, calculate {} ", code, xorVal);
        return xorVal.equals(code);
    }

    public static void printlnLog(DialogueInfo info) {
        StaticLog.info("Receives {} Bytes", info.getLength());
        StaticLog.info("TID {} Terminal {} LID {} ", info.getTid(), info.getTnr(), info.getLaneId());
        StaticLog.info("LPN {} ", info.getLpn());
        StaticLog.info("VID {} ", info.getVid());
        StaticLog.info("CMD {} ", info.getCmd());
        StaticLog.info("Ticket Type {} ", info.getTicketType());
        StaticLog.info("Barrier {} ", info.getBarrierStatus());
        StaticLog.info("Date {} Time {}", info.getSysDate(), info.getSysTime());
        StaticLog.info("CMode {} ", info.getCorrectMode());
        StaticLog.info("Allow Receive DID {} 1 ", info.getTid());
        StaticLog.info("PresentFlag Timer On DID {} 1 ", info.getTid());
        StaticLog.info("Reply LPN, Lane 1 LPN ");
        StaticLog.info("Send_121_Date Lane 1 ");
    }

    public static String byteToString(byte[] bytes) {
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] != 0) {
                strBuilder.append((char) bytes[i]);
            } else {
                break;
            }
        }
        return strBuilder.toString();
    }

    public static int toInt(byte[] bytes) {
        int number = 0;
        for (int i = 0; i < bytes.length; i++) {
            number += bytes[i] << i * 8;
        }
        return number;
    }

    public static byte getXorVal(byte[] data) {
        byte temp = data[0];
        for (int i = 1; i < data.length; i++) {
            temp ^= data[i];
        }
        return temp;
    }

}
